package final2107.night.day05;

public class ReverseSentence {
    public static void main(String[] args) {
        String str = "I love the game";
        String sep = " ";
        String result = reverseSentence(str,sep);
        System.out.println(result);
    }
    public static  String reverseSentence(String in,String sep){
        int index = in.length()-1;  //index即表示查找的起始下标，也表示查找到的分隔符的下标
        int start,end=in.length();
        StringBuilder builder = new StringBuilder();

        while ((index=in.lastIndexOf(sep,index))!=-1){
            //表示找到分隔符
            start = index+sep.length();
            String sub = in.substring(start,end);
            builder.append(sub).append(sep);
            //修改end的位置
            end = index;
            index--;
        }
        String first = in.substring(0,end);
        return builder.append(first).toString();
    }
}
